package hasGroupsSizeX;

import java.util.HashMap;
import java.util.Map;

public class Solution {
    public boolean hasGroupsSizeX(int[] deck) {
        Map<Integer,Integer> map = new HashMap<>();

        for (int i = 0; i < deck.length; i++) {
            map.put(deck[i],map.getOrDefault(deck[i],0)+1);
        }
        int t = 0;

        // 求多个元素的最大公因数
        for (int v: map.values()) {
            t = gcd(t,v);
        }
        return t>=2;
    }
    private int gcd(int a,int b){
        // 辗转相除法
        while(b!=0){
            int mod = a%b;
            a = b;
            b = mod;
        }
        return a;
    }

}
